﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace C7
{
    /// <summary>
    /// Interaction logic for MarqueeText.xaml
    /// </summary>
    public partial class MarqueeText : UserControl
    {
        public MarqueeText()
        {
            this.InitializeComponent();
        }

        public MarqueeStuff ms = new MarqueeStuff();

        private void Marquee_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {       
                ms.Intialize(rectBackground, tbText);
                ms.ran = true;
        }

        private void MarqueeMouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            ms.sb.Pause();
        }

        private void MarqueeMouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (ms.ran)
            {
                ms.sb.Resume();
            }
        }

        #region TextProperty
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        public static readonly DependencyProperty TextProperty =
                                                DependencyProperty.Register(
                                                                            "Text",
                                                                            typeof(string),
                                                                            typeof(MarqueeText),
                                                                            new FrameworkPropertyMetadata(
                                                                            new PropertyChangedCallback(ChangeText)));

        private static void ChangeText(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            (source as MarqueeText).UpdateText((e.NewValue as string));
        }

        private void UpdateText(string newText)
        {
            tbText.Text = newText;
        }
        #endregion
    }
}

public class MarqueeStuff
{
    public  DoubleAnimation daMarquee = new DoubleAnimation();
    public  bool ran = false;
    public  Storyboard sb = new Storyboard();
    public  void Intialize(Rectangle rectBackground, TextBlock tbText)
    {
        daMarquee.From = -tbText.ActualWidth;
        daMarquee.To = rectBackground.ActualWidth;
        //ac = daMarquee.CreateClock();
        daMarquee.Duration = new Duration(TimeSpan.FromSeconds(35));
        daMarquee.RepeatBehavior = RepeatBehavior.Forever;
        Storyboard.SetTargetProperty(daMarquee, new PropertyPath(Canvas.RightProperty));
        Storyboard.SetTarget(daMarquee, tbText);
        sb.Children.Add(daMarquee);
        sb.Begin();
        //tbText.BeginAnimation(Canvas.RightProperty, daMarquee);
    }

}